<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$return['basic']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$serve=pdo_get('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['serve_id']),array('id','title','fileds','notice','serve_time','price','package'));
	$serve['fileds']=$serve['fileds']?json_decode($serve['fileds'],true):array();
	foreach($serve['fileds'] as &$sv){
		if($sv['value']){
			$sv['value']=explode(';',$sv['value']);
		}
	}
	$data=$this->days($_RQ['serve_id'],$return['basic']['days']);
	$worker=pdo_get('jy_yuyue_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['worker_id']));
	$serve['serve_time']=json_decode($serve['serve_time'],true);
	$serve_time=$serve['serve_time'][intval($_RQ['timeid'])];
	$serve['package']=json_decode($serve['package'],true);
	$package=$serve['package'][intval($_RQ['packageid'])];
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('nickname','mobile'));	
	$return['day']=$data[$_RQ['dayid']];
	$return['package']=$package;
	$return['serve']=$serve;
	$return['worker']=$worker;
	$return['serve_time']=$serve_time;
	$post=array(
		'serve_id'=>intval($_RQ['serve_id']),
		'timeid'=>intval($_RQ['timeid']),
		'packageid'=>intval($_RQ['packageid']),
		'worker_id'=>intval($_RQ['worker_id']),
		'dayid'=>intval($_RQ['dayid']),
		'package_price'=>$package['package_price']?:$serve['price'],
		'totalprice'=>$package['package_price']?:$serve['price'],
		'username'=>$member['nickname'],
		'mobile'=>$member['mobile'],
		'fileds'=>$serve['fileds'],
		'total'=>1,
		'serve_type'=>'0'
	);
	$return['post']=$post;
	exi($return);
}
if($_RQ['op']=='uporder'){
	$post=get_postdata();
	if(!$post){
		exi('信息错误，请重新提交','error');
	}
	$order_id=$this->session('order_id');
	$data=array('name'=>$post['name'],'mobile'=>$post['mobile'],'address'=>$post['address']);
	$data2=array('username'=>$post['name'],'mobile'=>$post['mobile'],'address'=>$post['address']);
	pdo_update('jy_yuyue_user',$data,array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$order=pdo_get('jy_yuyue_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
	$package=json_decode($order['package'],true);
	pdo_update('jy_yuyue_order',array('status'=>'1','pay'=>0),array('id'=>$order['id']));
	$message_tpl=$this->plugin_setting('message_tpl');
	if($message_tpl['isnew']){
		$this->model('notice')->notice_workermsg($order);
	}
	exi('预约成功！','success','jy_yuyue/order/list/status=1&id='.$order['id']);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$vip=$this->plugin_setting('vip');
	$isvip=pdo_getcolumn('jy_yuyue_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'isvip');
	if($vip['openvip']==1 && $isvip==0){
		exi('请先激活','error','jy_yuyue/buyvip/');
	}
	$serve_id = $postdata['serve_id'];
	$data=$this->days($serve_id,$return['basic']['days']);
	if(!$postdata['timeid']){
			//exi('请选择服务时间','error');
	}
	if(!$postdata['username']){
			exi('请输入联系人姓名','error');
	}
	if(!$postdata['mobile']){
			exi('请输入联系人手机号码','error');
	}  
	if(!preg_match("/^1[345678]{1}\d{9}$/",$postdata['mobile'])){  	 
			exi('请输入正确的手机号码','error');
	} 
	if($postdata['fileds']){
			foreach ($postdata['fileds'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
	}
	$this->setting['yuyue_num']=$this->setting['yuyue_num']?:1;
	if($this->setting['yuyue_num'] && $this->setting['yuyue_num']<$postdata['total']){
		exi('最多可预约'.$this->setting['yuyue_num'].'人','error');
	}
	//判断待支付订单及判断是否超过可预订次数
	$serve=pdo_get('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$serve_id));
	$serve_time=json_decode($serve['serve_time'],true);
	$postdata['day']=$data[$postdata['dayid']]['ymd'];
	$num=pdo_getsum('jy_yuyue_order',array('timeid'=>$postdata['timeid'],'uniacid'=>$_SESSION['uniacid'],'day'=>$postdata['day'],'serve_id'=>$postdata['serve_id'],'status'=>array(1,2,4)),'total');
	if(($serve_time[$postdata['timeid']]['yuyue'])<=$num){
		exi('该时间段预约已满，请重新选择预约时间段！','error');
	}
	if($serve['xianzhi']==1){
			//某天
			$todaysum=pdo_getsum('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'user_id'=>$_SESSION['uid'],'serve_id'=>$serve_id,'status'=>array(1,2,4),'day'=>$data[$postdata['dayid']]['ymd']),'total');
			if($todaysum>=$serve['daytimes'] && $serve['daytimes']){
				exi('你可预约该项目该日次数已达上限,请选择其他日期','error');
			}
			//所有
			$allsum=pdo_getsum('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'user_id'=>$_SESSION['uid'],'serve_id'=>$serve_id,'status'=>array(1,2,4)),'total');
			if($allsum>=$serve['alltimes'] && $serve['alltimes']){
				exi('你可预约总次数已达上限,请选择其他项目','error');
			}
		
		}
		$package=json_decode($serve['package'],true);
		if($postdata['packageid'] || $package[$postdata['packageid']]){
			$packageinfo=$package[$postdata['packageid']];
			$price = $packageinfo['package_price'];
		}else{
			$price = $serve['price'];
		}
		$totalprice=$price*$postdata['total'];
		$selfcount=pdo_count('jy_yuyue_order',array('serve_id'=>$postdata['serve_id'],'uniacid'=>$_SESSION['uniacid'],'day'=>$data[$postdata['dayid']]['ymd'],'timeid'=>$postdata['timeid']),'id');
		$daysnum=$serve_time[$postdata['timeid']]['times']['start'];
		$daysnum=str_replace(':','',$daysnum);
		$selfnumber=$daysnum.sprintf("%03d", $selfcount+1);
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'user_id'=>$_SESSION['uid'],
			'worker_id'=>$postdata['worker_id'],
			'serve_id'=>$postdata['serve_id'],
			'merchid'=>$serve['merchid'],
			'package'=>ijson_encode($package[$postdata['packageid']]),
			'money'=>$totalprice,
			'total'=>$postdata['total'],
			'timeid'=>$postdata['timeid'],
			'day'=>$data[$postdata['dayid']]['ymd'],
			'address'=>$postdata['address'],
			'username'=>$postdata['username'],
			'mobile'=>$postdata['mobile'],
			'ordersn'=>get_ordersn('jy_yuyue_order','ordersn'),
			'remark'=>$postdata['remark'],
			'fileds'=>ijson_encode($postdata['fileds']),
			'selfnumber'=>$selfnumber,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_yuyue_order',$insert);
		$id=pdo_insertid();
		if($totalprice>0){
			$params=array(
					'id'=>$id,
					'title'=>'预约订单',
					'fee'=>$totalprice,
					'plugin'=>'jy_yuyue',
					//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
					'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
					'back_url'=>'jy_yuyue/book/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
					'success_url'=>'jy_yuyue/order/',
					'error_url'=>'jy_yuyue/order/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			$this->pdo_update('order',array('pay_id'=>$payid),array('id'=>$id));
			$res=array(
					'pay_id'=>$payid,
					'order_id'=>$id,
			);
			exi($res);
		}else{
			$payresult=array('fee'=>0);
			$order=$insert;
			$order['id']=$id;
			$this->model('model')->payresult($order,$payresult);
			exi('预约成功！','','jy_yuyue/order/');
		}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_yuyue_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	$this->model('model')->payresult($order,$payresult);
	exi('支付成功','success','jy_yuyue/order/list/status=1&id='.$order['id']);
}